<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/22
 * Time: 17:54
 */
namespace app\business\controller;
use think\Db;
use think\Session;
class Postpayment extends Common
{
	/**
	 * 判断是否登录
	*/
	public function _initialize()
	{
		parent:: _initialize();
	}
	
	/**
	 * 寄售订单
	 * @param string get.ordernumber 要检索的订单号
	 * @param string get.status 订单的状态
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('postpayment')
			->field('postpayment.ordernumber,postpayment.date,shop.address,business.id')
			->join('shop','shop.id = postpayment.sid','LEFT')
			->join('business','business.id = postpayment.bid','LEFT')
			->where('bid',Session::get('business.id'))
			->group('postpayment.ordernumber')
			->order('postpayment.ordernumber desc');
		if(!empty(trim(input('get.ordernumber')))){
			$ordernumber = trim(input('get.ordernumber'));
			$db->where('ordernumber',$ordernumber);
			$where['query']['ordernumber'] = $ordernumber;
		}
		if(input('get.status')){
			$status = trim(input('get.status'));
			$db->where('postpayment.status',$status);
			$where['query']['status'] = $status;
		}else{
			$db->whereNotIn('postpayment.status',[0,1]);
		}
		$result = $db->paginate(8,false,$where);
		return view('index',compact('result','ordernumber'));
	}
	
	/**
	 * 详情
	 * @param string get.ordernumber 订单号
	*/
	public function detail()
	{
		$where = ['query'=>[]];
		$ordernumber = input('get.ordernumber');
		$where['query']['ordernumber'] = $ordernumber;
		$result = Db::table('postpayment')
				->field('product.pname,product.purchaseprice,postpayment.number,postpayment.total,postpayment.express,postpayment.oddnumber,
							postpayment.describe,postpayment.status,postpayment.id,postpayment.ordernumber')
				->join('product','product.id = postpayment.pid','LEFT')
				->join('business','business.id = postpayment.bid','LEFT')
				->where('postpayment.ordernumber',$ordernumber)
				->where('business.id',Session::get('business.id'))
				->paginate(8,false,$where);
		return view('detail',compact('result'));
	}
	/**
	 * 描述
	 * @param string get.id 订单id
	*/
	public function describe()
	{
		$id = input('get.id');
		$describe = Db::table('postpayment')->where('id',$id)->value('describe');
		return view('describe',compact('describe'));
	}
	
	/**
	 * 订单详情
	 * @param string get.id 订单号
	*/
	public function orderdetail()
	{
		$id = input('get.id');
		$result = Db::table('postpayment')
			->field('product.pname,product.category,product.introduce,product.purchaseprice,
						product.image,postpayment.ordernumber,postpayment.date,postpayment.number,
						postpayment.total,postpayment.images,postpayment.describe')
			->join('shop','shop.id = postpayment.sid','LEFT')
			->join('product','product.id = postpayment.pid','LEFT')
			->where('postpayment.id',$id)
			->where('postpayment.bid',Session::get('business.id'))
			->find();
		$result['image'] = json_decode($result['image']);
		return view('orderdetail',compact('result'));
	}
	
	/**
	 * 添加运单号和快递公司页面
	 * @param string post.id 采购单id
	*/
	public function express()
	{
		$id = input('get.id');
		return view('express',compact('id'));
	}
	/**
	 * 添加运单号和快递公司
	 * @param string post.id 采购单id
	 * @param string post.number 运单号
	 * @param string post.express 快递公司
	 */
	public function logistics()
	{
		if(request()->isPost()){
			try{
				$id = input('post.id');
				$input['oddnumber'] = input('post.oddnumber');
				$input['express'] = input('post.express');
				$input['status'] = 3;
				$result = Db::table('postpayment')->where('id',$id)->update($input);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				$success = ['success'=>'添加成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}